<template>
  <div class="bg">
    <nav-com
      :title="$t('linkserve')"
      :showView='showView'
      @back="backHandler()"
    ></nav-com>
    <div class="top-view1">
      <!--      链接服务-->
      <van-cell-group inset class="m-b-10">
        <van-cell inset is-link :title="$t('anzhuojiangkang')" v-if="isanzhuo">
          <template #right-icon>
            <van-switch v-model="checked" @change="changenotice($event)" active-color="#00DEA1" inactive-color="#fafafa" />
          </template>
        </van-cell>
        <van-cell inset is-link :title="$t('iosjiangkang')" v-if="!isanzhuo">
          <template #right-icon>
            <van-switch v-model="checked" @change="changenotice($event)" active-color="#00DEA1" inactive-color="#fafafa" />
          </template>
        </van-cell>
      </van-cell-group>
      <!--      弹框-->
      <van-dialog v-model="showchangefuwu" v-if="isanzhuo" :title="$t('linkstishi')" show-cancel-button @confirm="confirm" @cancel="cancel" :confirm-button-text="$t('yes')" :cancel-button-text="$t('no')">
      </van-dialog>
      <van-dialog v-model="showchangefuwu" v-if="!isanzhuo" :title="$t('ioslinkstishi')" show-cancel-button @confirm="confirm" @cancel="cancel" :confirm-button-text="$t('yes')" :cancel-button-text="$t('no')">
      </van-dialog>
      <van-popup
        round
        v-model="showchangename"
        position="center"
      >
        <div class="rili-content">
         <div class="linksuccess"> {{ $t('linkssucces')}}</div>
        </div>
      </van-popup>
    </div>
  </div>
</template>
<script>
import { Toast } from 'vant'
import navCom from '@/components/nav-com.vue'
import { modifypwd, profile, userInfo } from '../../api/user'
export default {
  name: 'bonusList',
  components: {
    navCom
  },
  data () {
    return {
      checked: '',
      showView: true,
      showPage: true,
      showchangename: false,
      lichengValue: '',
      shareUser: {},
      passwords: '',
      oldpasswords: '',
      isanzhuo: true,
      showchangefuwu: false
    }
  },
  mounted () {
    const nav = navigator.userAgent
    if (nav.indexOf('Android') > -1 || nav.indexOf('Adr') > -1) {
      this.isanzhuo = true
    } else if (nav.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
      this.isanzhuo = false
    }
    this.init()
  },
  methods: {
    loginout () {

    },
    todeviceManagement () {
      this.jumpTo('/deviceManagement', { canshu: 'settings' })
    },
    tolangqiehuan () {
      this.jumpTo('/langqiehuan')
    },
    confirm () {
      const nav = navigator.userAgent
      if (nav.indexOf('Android') > -1 || nav.indexOf('Adr') > -1) {
        let res = phone.getHealthServe()
        let datas = JSON.parse(res)
        if (datas.openHealth) {
          this.showchangename = true
        } else {
          this.checked = false
        }
      } else if (nav.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
        this.$bridge.callhandler('getHealthServe', {}, res => {
          let datas = res
          if (datas.openHealth) {
            this.showchangename = true
          } else {
            this.checked = false
          }
        })
      }
      this.changegugehealth()
      setTimeout(() => {
        this.showchangename = false
      }, 1500)
    },
    cancel () {
      this.checked = false
    },
    changegugehealth () {
      profile({ googleHealth: this.checked ? '1' : '0' }).then((res) => {
        if (res.code == 1) {
          if (this.$i18n.locale == 'zh') {
            this.$toast('修改成功')
          } else {
            this.$toast('Changed')
          }
          const nav = navigator.userAgent
          if (nav.indexOf('Android') > -1 || nav.indexOf('Adr') > -1) {
            phone.userInfoEdit()
          } else if (nav.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
            setTimeout(() => {
              this.$bridge.callhandler('userInfoEdit', {}, res => {})
            })
          }
        }
      })
    },
    changenotice (e) {
      console.log('=======333', e)
      if (e) {
        this.showchangefuwu = true
      } else {
      }
      console.log('=======333', this.checked)
    },
    jumpTo (url, canshu) {
      this.$router.push({ path: url,
        query: { canshu: canshu }
      })
    },
    // 返回
    backHandler () {
      this.$router.go(-1)
    },
    // 初始化
    init () {
      this.getUserData()
    },
    // 用户信息
    getUserData () {
      userInfo().then((res) => {
        if (res.code == 1) {
          this.shareUser = res.data.userinfo
          if (this.shareUser.googleHealth == '1') {
            this.checked = true
          } else if (this.shareUser.googleHealth == '0') {
            this.checked = false
          }
          console.log('== this.shareUser', this.shareUser.notice, this.checked)
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.top-view1{
  margin-top: 26.23vw;
}
.m-b-10{
  margin-bottom: 10px;
}
.m-t-10{
  margin-top: 10px;
}
.van-cell__title, .van-cell__value {
  flex: 1;
  display: flex;
  align-items: center;
}
.rili-content{
  width: 90vw;
  background: #fff;

}
.linksuccess{
  padding: 10px;
  font-size: 22px;
  font-weight: 600;
  color: #303030;
  display: flex;
  align-items: center;
  justify-content: center;
}
::v-deep .van-cell{
  font-size:20px!important;
}
</style>
